#include <iostream>

using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
private:
    void reverse(ListNode* head) {
        ListNode* begin = head->next;
        ListNode* end = head->next;
        ListNode* node = head->next->next;
        while (node != nullptr) {
            end->next = node->next;
            head->next = node;
            node->next = begin;
            begin = node;
            node = end->next;
        }
    }
public:
    bool isPalindrome(ListNode* head) {
        ListNode* node = head;
        int count = 0;
        while (node != nullptr) {
            count++;
            node = node->next;
        }
        if (count <= 1) {
            return true;
        }
        int halt_len = count / 2;
        node = head;
        for (int i = 0; i < halt_len - 1; i++) {
            node = node->next;
        }
        if (halt_len < (float) count / 2) {
            node = node->next;
        }
        reverse(node);
        ListNode* node1 = node->next;
        node = head;
        for (int i = 0; i < halt_len; i++) {
            if (node->val != node1->val) {
                return false;
            }
            node = node->next;
            node1 = node1->next;
        }
        return true;
    }
};

int main() {
    ListNode l1(1), l2(2), l3(1), l4(1);
    l1.next = &l2;
    l2.next = &l3;
    //l3.next = &l4;
    Solution solution;
    bool output = solution.isPalindrome(&l1);
    cout << output << endl;
    return 0;
}